const app = getApp();
let conf = require('../../app.config').app
const { formatDate } = require('../../common/js/utils');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    // 性别选择框
    genderPicker: [ ' ', '男', '女' ],
    // 性别集合
    genderList: [
      { id: 0, title: ' ' },
      { id: 1, title: '男' },
      { id: 2, title: '女' }
    ],
    headimgurl:'',
    // 姓名
    name: '',
    // 性别
    gender: 0,
    // 生日
    birthday: '',
    // 生日可选择的最后日期
    birthdayEndDate: '',
    // 手机号码
    mobileNumber: '',
    // 初始手机号码
    originalMobileNumber: '',
    // 验证码
    verificationCode: '',
    // 省市区
    region: [null, null, null],
    // 省份
    province: '',
    // 城市
    city: '',
    // 行政区
    district: '',
    // 具体地址
    address: '',
    // 是否允许修改生日和性别
    isUpdate: false,
    // 手机号码没有变化时，隐藏验证码
    verifyingHide: true,
    // 获取验证码
    verification: '',
    // 是否正在倒计时
    verifying: false,
    // 获取验证码的文字
    verificationText: '获取验证码',
    userinfo: wx.getStorageSync('userinfo'),
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    let that = this
    that.setData({
      alert: that.selectComponent("#alert"),
    });

  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    const that = this;
    that.onLoadData(that);
  },

  // 加载页面数据
  onLoadData(that) {
    this.api.interface.getCustomerByOpenId().then(result=>{
      console.log(result,1111111111111)
      that.setData({
        birthdayEndDate: formatDate(new Date().toDateString()),
      });
      if (result.data.data.nickName) {
        that.setData({
          name: result.data.data.nickName,
        });
      }
      if (result.data.data.headimgurl) {
        that.setData({
          headimgurl: result.data.data.headimgurl,
        });
      }
      if (result.data.data.gender) {
        that.setData({
          gender: result.data.data.gender,
        });
      }
      if (result.data.data.birthday) {
        that.setData({
          birthday: formatDate(result.data.data.birthday),
        });
      }
      if (result.data.data.mobile) {
        that.setData({
          mobileNumber: result.data.data.mobile,
          originalMobileNumber: result.data.data.mobile,
        });
      }
      console.log(result.data.data.province,result.data.data.city,result.data.data.district,22222222222)
      if (result.data.data.province) {
        that.setData({
          province: result.data.data.province,
        });
      }
      if (result.data.data.city) {
        that.setData({
          city: result.data.data.city,
        });
      }
      if (result.data.data.district) {
        that.setData({
          district: result.data.data.district,
        });
      }
      console.log(1)
      //   if (result.data.isUpdate) {
      that.setData({
        isUpdate: result.data.data.isUpdate ? result.data.data.isUpdate : 1,
      });
      //   }
      if (result.data.data.province && result.data.data.city && result.data.data.district) {
        that.setData({
          region: [result.data.data.province, result.data.data.city, result.data.data.district],
        });
      }
    })
  },
  // 性别选择
  onGenderChange(event) {
    this.setData({
      gender: event.detail.value
    });
  },

  // 生日选择
  onBirthdayChange(event) {
    console.log(event,111111111111111)
    this.setData({
      birthday: event.detail.value
    });
  },

  // 手机号码输入
  onMobileNumberInput(event) {
    this.setData({
      mobileNumber: event.detail.value
    });
    if (this.data.mobileNumber === this.data.originalMobileNumber) {
      this.setData({
        verifyingHide: true,
        verificationCode: ''
      });
    }
    else {
      this.setData({
        verifyingHide: false,
        verificationCode: ''
      });
    }
  },

  // 验证码
  onVerificationCodeTap(event) {
    this.setData({
      verificationCode: event.detail.value
    });
  },
  onNicknameInput(e){
    this.setData({
      name: e.detail.value,
      nickName: e.detail.value
    });
  },
  // 省市区选择
  onRegionChange(event) {
    this.setData({
      // 省市区
      region: event.detail.value,
      // 省份
      province: event.detail.value[0],
      // 城市
      city: event.detail.value[1],
      // 行政区
      district: event.detail.value[2],
    })
  },
  // 提交
  onSubmit(event) {
    var that = this;
    var verificationCode = '0000';
    // 验证是否输入了姓名
    if (!that.data.name) {
      that.data.alert.show({
        content: '请输入姓名',
      });
      return false;
    }
    // 验证是否选择了性别
    if (that.data.isUpdate) {
      if (0 >= that.data.gender) {
        that.data.alert.show({
          content: '请选择性别',
        });
        return false;
      }
      // 验证是否选择了生日
      if (!that.data.birthday) {
        that.data.alert.show({
          content: '请选择生日',
        });
        return false;
      }
    }
    // 验证是否输入了手机号码
    if (!that.data.mobileNumber) {
      that.data.alert.show({
        content: '请输入手机号码',
      });
      return false;
    }
    // 验证手机号码格式是否正确
    if (!(/^1\d{10}$/.test(that.data.mobileNumber))) {
      that.data.alert.show({
        content: '手机号码格式不正确',
      });
      return false;
    }
    // 需要验证码
    if (!that.data.verifyingHide) {
      // 验证是否输入了验证码
      if (!that.data.verificationCode) {
        that.data.alert.show({
          content: '请输入验证码',
        });
        return false;
      }
      // 验证验证码格式是否正确
      if (!(/^\d{4}$/.test(that.data.verificationCode))) {
        that.data.alert.show({
          content: '验证码格式不正确',
        });
        return false;
      }
      verificationCode = that.data.verificationCode;
    }
    // 显示载入
    wx.showLoading({});
    console.log(that.data.birthday,'that.data.birthday  -- - --that.data.birthday')
    // 请求接口提交
    this.api.interface.updateCustomer({
      openId: app.globalData.openid,
      name: that.data.name,
      nickName: that.data.name,
      headimgurl:that.data.headimgurl,
      gender: that.data.gender,
      birthday: that.data.birthday,
      mobile: that.data.mobileNumber,
      verificationCode: verificationCode,
      province: that.data.province,
      city: that.data.city,
      district: that.data.district,
      address: that.data.address,
    }).then(res=>{
      wx.navigateBack({
        delta: 1,
      })
    })
  },
  bindchooseavatar(e) {
    let t = this;
    // 临时路径 e.detail.avatarUrl
    t.setData({
      headimgurl: e.detail.avatarUrl
    })
    t.api.interface.uploadCustomerHeardImgFile(e.detail.avatarUrl, (res) => {
      if(res.code == 200){
        t.setData({
          headimgurl: res.data
        });
      }
    });
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})